package com.example.fw;

import com.example.tests.AddressData;

import com.example.tests.PhoneData;
import com.example.utils.ListOf;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: pshevchenko
 * Date: 19.03.13
 * Time: 12:52
 * To change this template use File | Settings | File Templates.
 */
public class PrintPhoneHelper extends WebDriverHelperBase {

    private ListOf<PhoneData> cachedPhoneList;

    private void rebuildCache() {
        cachedPhoneList = new ListOf<PhoneData>();
        List<WebElement> addressBlocksList = driver.findElements(By.xpath("//tbody/tr/td[@valign=\"top\"]"));
        int addressCount = addressBlocksList.size();
        for (int i = 0; i <= addressCount / 3; i ++){
            for (int j = 0; (j < 3) && (j + (i * 3) < addressCount); j++)
            {
                AddressData addressData = getAddressFromPrintForm(i, j);
                if (addressData != null)
                    cachedPhoneList.add(new PhoneData(addressData));
                else cachedPhoneList.add(null);
            }//for j
        }// for i
    }

    private boolean convertPrintStringToAttributeValue(AddressData addressData, String inputString){
        String analyzedString = "" + inputString;
        if ((analyzedString.matches("H: .+")) && (("" + addressData.getHomePhone()).length() == 0)){
            addressData.setHomePhone(analyzedString.substring("H: ".length()));
        }
        else if ((analyzedString.matches("M: .+")) && (("" + addressData.getCellPhone()).length() == 0)){
            addressData.setCellPhone(analyzedString.substring("M: ".length()));
        }
        else if ((analyzedString.matches("W: .+")) && (("" + addressData.getWorkPhone()).length() == 0)){
            addressData.setWorkPhone(analyzedString.substring("W: ".length()));
        }
        else if ((analyzedString.matches("P: .+")) && (("" + addressData.getHomePhone2()).length() == 0)){
            addressData.setHomePhone2(analyzedString.substring("P: ".length()));
        }
        else if ((analyzedString.matches("Birthday: .+")) && (("" + addressData.getBirthDate()).length() == 0)){
            analyzedString = analyzedString
                    .substring("Birthday: ".length())
                    .replaceFirst("\\.", "");
            addressData.setBirthDate(analyzedString);
        }
        else if ((analyzedString.matches(".* :")) && (addressData.getFullName().length() == 0)){
            //Empty name
            addressData.setFirstName(analyzedString.substring(0, analyzedString.length() - (" :".length())));
        }
        else if ((analyzedString.matches(".*:")) && (addressData.getFullName().length() == 0)){
            addressData.setFirstName(analyzedString.substring(0, analyzedString.length() - (":".length())));
        }
        else if (("" + analyzedString).length() > 0)
            return false;
        return true;
    }

    private AddressData getAddressFromPrintForm(int rowIndex, int cellIndex) {
        AddressData resultAddress = new AddressData();
        String unWorkedCellData = getFieldText(By.xpath("//tbody/tr[" + (rowIndex + 1) + "]/td[" + (cellIndex + 1) + "]"));

        String[] splittedCellData = unWorkedCellData.split("\n");
        for (String nextString : splittedCellData){
            if (!convertPrintStringToAttributeValue(resultAddress, nextString))
                return null;
        }
        return resultAddress;
    }

//----------------------------------------------------------------------------------------------------------------------

    public int checkWrongAddressCount(){
        getPhoneList();
        return getWrongAddressCount();
    }

//----------------------------------------------------------------------------------------------------------------------

    public PrintPhoneHelper(ApplicationManager manager){
        super(manager);
    }

    public PrintPhoneHelper reportAddressModification()
    {
        cachedPhoneList = null;
        return this;
    }


    public ListOf<PhoneData> getPhoneList() {
        if (cachedPhoneList == null)
            rebuildCache();
        return cachedPhoneList;
    }

    public int getWrongAddressCount(){
        int result = 0;

        if (cachedPhoneList == null)
            return 0;

        for(PhoneData phoneData : cachedPhoneList)
            if (phoneData == null)
                result ++;

        return result;
    }
}
